import java.util.concurrent.CountDownLatch;

/**
 * Created by admin on 2021/3/31.
 */
// 1.CountDownLatch 应用场景  先处理某个大任务 再处理100个小任务处理完之后
public class CountDownLatchTest1 {

    public static void main(String[] args) throws InterruptedException {
        final CountDownLatch countDownLatch = new CountDownLatch(3);
        new Thread(new Runnable() {
            public void run() {
                System.out.println("T1 start");
                try {
                    countDownLatch.await();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("T1 STOP");
            }
        }, "t1").start();
        new Thread(new Runnable() {
            public void run() {
                System.out.println("T2 start");
                try {
                    countDownLatch.await();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("T2 STOP");
            }
        }, "t2").start();

        Thread.sleep(2000);

        new Thread(new Runnable() {

            public void run() {

                try {
                    Thread.sleep(1000);
                    System.out.println("T1 countDown1");
                    countDownLatch.countDown();

                    Thread.sleep(1000);
                    System.out.println("T2 countDown2");
                    countDownLatch.countDown();

                    Thread.sleep(1000);
                    System.out.println("T3 countDown3");
                    countDownLatch.countDown();

                    System.out.println("T3 STOP");
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }, "t3").start();

    }

}
